package com.hiema.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.hiema.entity.ApUnlikesBehavior;
import com.hiema.mapper.ApUnlikeMapper;
import com.hiema.service.BehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class BehaviorServiceImpl extends ServiceImpl<ApUnlikeMapper, ApUnlikesBehavior> implements BehaviorService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 不喜欢
     * @param undto
     */
    @Override
    public ResponseResult unlike(UnLikesBehaviorDto undto) {
        //判断文章id是否存在
        if (undto.getArticleId() == null) {
            return ResponseResult.error(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer userId = UserThreadLocalUtils.getUserId();
        //判断用户是否存在
        if (userId == null) {
            return ResponseResult.error(AppHttpCodeEnum.NEED_LOGIN);
        }
        //判断是否是喜欢或者取消不喜欢
        if (undto.getType() == 0) {
            redisTemplate.opsForHash().put(undto.getArticleId(), "like", undto.getType());
        } else {
            redisTemplate.opsForHash().put(undto.getArticleId(), "unlike", undto.getType());
        }
        //返回成功的消息
        return ResponseResult.ok(AppHttpCodeEnum.SUCCESS);
    }
}
